<template>
  <el-form
    ref="systemForm"
    :model="formData"
    :rules="formRules"
    label-width="90px"
    label-position="right"
  >
    <el-form-item v-if="action === 'edit'" prop="id">
      <input v-model="formData.id" type="hidden">
    </el-form-item>
    <el-form-item label="商户编码" prop="businessCode">
      <el-input v-model="formData.businessCode" type="text" placeholder="必填" />
    </el-form-item>
    <el-form-item label="商户名称" prop="businessName">
      <el-input v-model="formData.businessName" type="text" placeholder="必填" />
    </el-form-item>
    <el-form-item label="加密类型" prop="encryptType">
      <SecurityType v-model="formData.encryptType" @change="handleEnryptTypeChange" />
    </el-form-item>
    <el-form-item label="密钥长度" prop="encryptKeySize">
      <el-select v-model="formData.encryptKeySize" placeholder="请选择">
        <el-option
          v-for="item in keySizeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="warning" @click="handleGennerateKey">生成密钥</el-button>
    </el-form-item>
    <el-form-item label="密钥/私钥" prop="encryptKey">
      <el-input v-model="formData.encryptKey" type="textarea" :autosize="{ minRows: 3 }" placeholder="密钥必填" />
    </el-form-item>
    <el-form-item label="密钥/公钥" prop="encryptKeyExt">
      <el-input v-model="formData.encryptKeyExt" type="textarea" :autosize="{ minRows: 3 }" placeholder="当为RSA算法时必填" />
    </el-form-item>

  </el-form>
</template>
<script>
import SecurityType from '@/components/admin/security-type';
import { get, save, update, gennerateKey } from '@/api/admin/security';

const aesKeySizes = [{
  label: '128位',
  value: 128
}, {
  label: '192位',
  value: 192
}, {
  label: '256位',
  value: 256
}];
const rsaKeySizes = [{
  label: '1024位',
  value: 1024
}];

export default {
  name: 'SecurityFormDialog',
  components: {
    SecurityType
  },
  props: {
    action: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      formRules: {
        businessCode: [
          { required: true, message: '编码必填！', trigger: 'blur' }
        ],
        businessName: [
          { required: true, message: '名称必填！', trigger: 'blur' }
        ],
        encryptType: [
          { required: true, message: '类型必填！', trigger: 'blur' }
        ],
        encryptKeySize: [
          { required: true, message: '长度必填！', trigger: 'blur' }
        ],
        encryptKey: [
          { required: true, message: '密钥必填！', trigger: 'blur' }
        ]
      },
      formData: {
        businessCode: null,
        businessName: null,
        encryptType: null,
        encryptKeySize: null,
        encryptKey: null,
        encryptKeyExt: null
      },
      keySizeOptions: []
    };
  },
  watch: {
    'formData.encryptType'(val) {
      // this.formData.encryptKeySize = null;
      if (val === '0') {
        this.keySizeOptions = rsaKeySizes;
      } else if (val === '2') {
        this.keySizeOptions = aesKeySizes;
      } else {
        this.keySizeOptions = null;
      }
    }
  },
  methods: {
    getInfo(id) {
      get(id).then(response => {
        const result = response.data;
        if (result.code === 0) {
          this.formData = result.data;
          this.formData.encryptType = this.formData.encryptType + '';
        } else {
          this.$notify.error({
            title: result.code,
            message: result.msg
          });
        }
      });
    },
    handleSubmit() {
      this.$refs.systemForm.validate((valid) => {
        if (valid) {
          if (this.action === 'add') {
            save(this.formData).then(response => {
              this.handleResult(response.data);
            });
          } else if (this.action === 'edit') {
            update(this.formData).then(response => {
              this.handleResult(response.data);
            });
          }
        }
      });
    },
    handleResult(obj) {
      if (obj.code === 0) {
        this.$notify({
          title: '提示',
          message: obj.msg,
          type: 'success'
        });
        this.$parent.$parent.customFormInitOption.showModal = false;
        this.reset();
        this.$parent.$parent.$refs.customTable.query();
      } else {
        this.$notify({
          title: '提示',
          message: obj.msg,
          type: 'error'
        });
      }
    },
    reset() {
      this.$refs.systemForm.resetFields();
    },
    handleGennerateKey() {
      if (this.formData.encryptType != null && this.formData.encryptKeySize != null) {
        const params = {
          encryptType: this.formData.encryptType,
          keySize: this.formData.encryptKeySize
        };
        gennerateKey(params).then(response => {
          const result = response.data;
          if (result.code === 0) {
            this.formData.encryptKey = result.data.encryptKey;
            this.formData.encryptKeyExt = result.data.encryptKeyExt;
          } else {
            this.$notify.error({
              title: result.code,
              message: result.msg
            });
          }
        });
      } else {
        this.$alert('加密类型与密钥长度必填', '提示');
      }
    }
  }
};
</script>
